<?php
/**
 * Created by PhpStorm.
 * User: zhixuan
 * Date: 2019/11/19
 * Time: 23:08
 */

namespace app\admin\model;


use app\common\enum\PrivilegeEnum;

class Privilege extends \app\common\model\Privilege
{

    /*
     *获取菜单列表
     * @return array;
    */
    public static function getTree(){
        $arr = self::getAll()->each(function($item){
            $item['is_menu'] = PrivilegeEnum::getName('is_menu',$item['is_menu'],true);
        })->toArray();
        return self::_sort($arr);
    }


    /*
     *获取菜单树
     *  */
    public static function getRolePrivilege(){
        $arr = self::getAll([],['pri_order'=>'desc'],'id,parent_id as parent,pri_name as text')->toArray();
        return self::_sort($arr);
    }

    /*
     * 获取上级ID递归排序
     * */
    public static function _family($arr,$cat_id=0,$isClear=true){
        static $data = array();
        if($isClear){
            $data = array();
        }
        foreach($arr as $k=>$v){
            if($v['id'] == $cat_id){
                self::_family($arr,$v['parent_id'],false);
                $data[] = $v;
            }
        }
        return $data;
    }

    /*
     * 根据当前ID获取所有上级
     * */
    public static function getFamily($cat_id){
        $arr = self::getAll();
        return self::_family($arr,$cat_id);
    }

    /*
     * 递归排序获取所有子栏目
     * $param array $arr
     * $param $parent_id int
     * $param $lev int
     * $param $isClear boolean
     * */
    public static function _sort($arr,$parent_id=0,$lev=1,$isClear=true){
        static $tree = [];
        if($isClear==true){
            $tree = [];
        }

        foreach($arr as $k=>$v) {
            if ($v['parent_id'] == $parent_id) {
                $v['name'] = $v['pri_name'];
                $v['lev'] = $lev;
                $tree[] = $v;
                self::_sort($arr,$v['id'],$lev+1,false);
            }
        }
        return $tree;
    }


    /*
     * 获取菜单
     *@param mix
     *@return array
     * */
    public static function getPrivilegeMenu2($where = []){
        //取出所有的权限并递归排序
        $static = new static;
        $_pri_data = $static->getAll($where);

        $pri_menu = [];
        foreach ($_pri_data as $k => $v) {
            if ($v['parent_id'] == 0) {

                $v['icon'] = "fa fa-list";

                foreach ($_pri_data as $k1 => $v1) {
                    if ($v1['parent_id'] == $v['id']) {
                        $v1['icon'] = 'fa fa-list';
                        $v['children'][] = $v1;
                    }
                }
                $pri_menu[] = $v;
            }
        }
        return $pri_menu;
    }

    /*
     * 获取菜单
     *@param mix
     *@return array
     * */
    public static function getPrivilegeMenu($arr,$parent_id=0,$lev=1){
        //取出所有的权限并递归排序
        $tree = [];
        foreach($arr as $k=>$v) {
            if ($v['parent_id'] == $parent_id) {
                $v['lev'] = $lev;
                $v['children'] = self::getPrivilegeMenu($arr,$v['id'],$lev+1);
                if($v['is_menu'] == 0){
                    continue;
                }
                $tree[] = $v;
            }
        }
        return $tree;
    }


}